package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.admin.goods.response.GoodsResponse;
import com.fram.bean.admin.goods.response.GoodsSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.qiniu.QiniuUtils;
import com.fram.entity.ProjectGoods;
import com.fram.entity.ProjectGoodsPrice;
import com.fram.mapper.ProjectGoodsMapper;
import com.fram.mapper.ProjectGoodsPriceMapper;
import com.fram.service.admin.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private ProjectGoodsMapper projectGoodsMapper;

	@Autowired
	private ProjectGoodsPriceMapper projectGoodsPriceMapper;

	@Autowired
	private QiniuUtils qiniuUtils;

	/** 增加商品 **/
	@Override
	public Long  insertProjectGoods(ProjectGoods request) throws ProException {
		if (!StringUtils.isEmpty(request.getLogo())) {
			request.setLogo(qiniuUtils.uploadImageUrl(request.getLogo()));
		}
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getDistributorId() + "/"));
		request.setAfterSaleService(
				qiniuUtils.dealHtml(request.getAfterSaleService(), "1/" + request.getDistributorId() + "/"));
		request.setProductFeatures(
				qiniuUtils.dealHtml(request.getProductFeatures(), "1/" + request.getDistributorId() + "/"));
		if (projectGoodsMapper.insertSelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		return request.getId();
	}

	/** 商品展示（含有模糊查询，分页查询，类型查询） **/
	@Override
	public PageInfo<GoodsSearchResponse> getProjectGoodsList(PageBean<ProjectGoods> request) throws ProException {
		// 分页无忘加
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsSearchResponse> list = projectGoodsMapper.listForAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	/** 商品详情查询 **/
	@Override
	public GoodsResponse selectById(Long id) throws ProException {
		GoodsResponse request = projectGoodsMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	/** 商品 修改 **/
	@Override
	public void updateProjectGoods(ProjectGoods request) throws ProException {
		ProjectGoods goods = projectGoodsMapper.selectByPrimaryKey(request.getId());
		if (goods == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		if (!StringUtils.isEmpty(request.getLogo())) {
			request.setLogo(qiniuUtils.uploadImageUrl(request.getLogo()));
		}
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + goods.getDistributorId() + "/"));
		request.setAfterSaleService(
				qiniuUtils.dealHtml(request.getAfterSaleService(), "1/" + goods.getDistributorId() + "/"));
		request.setProductFeatures(
				qiniuUtils.dealHtml(request.getProductFeatures(), "1/" + goods.getDistributorId() + "/"));
		if (projectGoodsMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	/** 商品（价格详情） **/
	@Override
	public ProjectGoodsPrice detailsPrice(Long id) throws ProException {
		ProjectGoodsPrice request = projectGoodsPriceMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	/** 商品（价格列表） **/
	@Override
	public PageInfo<ProjectGoodsPrice> listPrice(PageBean<ProjectGoodsPrice> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<ProjectGoodsPrice> list = projectGoodsPriceMapper.selectByGoodsIds(request.getParam().getGoodsId());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	/** 商品（添加价格） **/
	@Override
	public void addPrice(ProjectGoodsPrice request) throws ProException {
		if (projectGoodsPriceMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	/** 商品（修改价格） **/
	@Override
	public void updatePrice(ProjectGoodsPrice request) throws ProException {
		if (projectGoodsPriceMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void updatePriceList(List<ProjectGoodsPrice> request) throws ProException {
		// 批量修改
		for (ProjectGoodsPrice projectGoodsPrice : request) {
			if (projectGoodsPriceMapper.updateByPrimaryKeySelective(projectGoodsPrice) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}
}
